package com.cn.jms;

import com.cn.http.HttpClients;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

public class ReceiveMq implements MessageListener {

    /**
     * 日志
     */
    private Logger LOGGER = LoggerFactory.getLogger(ReceiveMq.class);
    /**
     * HttpClient
     */
    private HttpClients httpClients;

    public HttpClients getHttpClients() {
        return httpClients;
    }

    public void setHttpClients(HttpClients httpClients) {
        this.httpClients = httpClients;
    }


    @Override
    /**
     * 接收消息
     */
    public void onMessage(Message msg) {
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(msg.getObjectProperty("payload").toString());
            }
        } catch (JMSException e) {
            LOGGER.error(e.getMessage(), e);
        }

        // text消息
        if (msg instanceof TextMessage) {
            TextMessage message = (TextMessage) msg;
            try {
                String data = message.getText();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("接收到ＭＱ的消息:" + data);
                }
            } catch (JMSException e) {
                throw new RuntimeException("JMSException", e);
            }
        }
        // 对象消息 projectSerialNumbe operators smsSendChannel
        else if (msg instanceof ObjectMessage) {
            ObjectMessage message = (ObjectMessage) msg;
            try {
                String payload = message.getStringProperty("payload");
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("接收到ＭＱ的消息:" + payload);
                }
                httpClients.aboutOperatorMessage(payload);

            } catch (JMSException e) {
                LOGGER.error(e.getMessage(), e);
            } catch (JSONException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        // map消息
        else if (msg instanceof MapMessage) {
            MapMessage message = (MapMessage) msg;
            try {
                Enumeration mapNames = message.getMapNames();
                while (mapNames.hasMoreElements()) {
                    String data = (String) mapNames.nextElement();
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("接收到ＭＱ的消息:" + message.getString(data));
                    }
                }
            } catch (JMSException e) {
                throw new RuntimeException("JMSException", e);
            }
        }
        // bytes消息
        else if (msg instanceof BytesMessage) {
            BytesMessage message = (BytesMessage) msg;
            byte[] buff = null;
            String data = null;
            try {
                long length = message.getBodyLength();
                buff = new byte[(int) length];
                message.readBytes(buff);
                data = new String(buff, "UTF-8");
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("接收到ＭＱ的消息:" + data);
                }
            } catch (JMSException e) {
                LOGGER.error(e.getMessage(), e);
            } catch (UnsupportedEncodingException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        // stream消息
        else if (msg instanceof StreamMessage) {
            StreamMessage message = (StreamMessage) msg;
            try {
                String data = message.readString();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("接收到ＭＱ的消息:" + data);
                }
            } catch (JMSException e) {
                LOGGER.error(e.getMessage(), e);
            }

        }
    }

}
